using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Rensoft.CRM.Client.AccountsAPI;

namespace Rensoft.CRM.Client.Editors
{
    public partial class CreditEditor : Rensoft.CRM.Client.Editors.BaseEditor
    {
        private Account account;

        protected CreditEditor()
        {
            InitializeComponent();
        }

        public CreditEditor(Program program)
            : base(program)
        {
            InitializeComponent();

            SetMode(EditorMode.Create);
            Text = "New Credit - " + AssemblyInfo.Title;
        }

        protected override void PerformClear()
        {
            base.PerformClear();

            /* Intentionally leave date the same.
             * Currency automatically resets. */

            recieveRadioButton.Checked = true;
            amountTextBox.ResetText();
            referenceTextBox.ResetText();
            detailsTextBox.ResetText();
        }

        protected override void PerformLoad(DoWorkEventArgs e)
        {
            base.PerformLoad(e);

            InitializeAccountComboBox(accountComboBox, AccountType.Client | AccountType.Supplier);

            switch (Mode)
            {
                case EditorMode.Modify:
                    {
                        SaveToolStripButton.Enabled = false;
                        SaveNewToolStripButton.Enabled = false;
                        SaveCloseToolStripButton.Enabled = false;
                    }
                    break;
            }
        }

        protected override void PerformSave(DoWorkEventArgs e)
        {
            base.PerformSave(e);

            CreditDirection direction = CreditDirection.Recieve;
            if (sendRadioButton.Checked)
            {
                direction = CreditDirection.Send;
            }

            decimal amount = decimal.Parse(amountTextBox.Text);

            AccountsService service = AccountsService.CreateInstance();

            switch (Mode)
            {
                case EditorMode.Create:
                    {
                        service.CreditCreate(
                            direction, amount, dateMonthCalendar.SelectionStart,
                            referenceTextBox.Text, detailsTextBox.Text, account.AccountId);
                    }
                    break;
            }
        }

        private void accountComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            account = ((AccountComboItem)accountComboBox.SelectedItem).Value;
            currencyTextBox.Text = account.Currency;
        }
    }
}

